home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Newbie question: Is this code OK?
- Date: 20 Jan 1996 19:56:15 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4drhcv$cr@news.iag.net>
- References: <4dmebk$foq@pegasus.interpac.net> <4doajv$ov0$2@mhafn.production.compuserve.com>
- NNTP-Posting-Host: pm1-orl30.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4doajv$ov0$2@mhafn.production.compuserve.com>,
- 71046.623@CompuServe.COM says...
- >
- >Why not just
- >
- > while (*string)
- > if (isspace(*string++)
- > numwords++;
- >
-
- Because, it will not behave, when odd spacing is encountered (and
- it is missing a ')' >:-).
-
- #include <stdio.h>
- #include <ctype.h>
-
- int main()
- {
- char *string, ary[] = " test string with odd spacing ";
- int numwords = 0;
-
- string = ary;
- while (*string)
- if (isspace(*string++)) /* missing ) */
- numwords++;
-
- printf( "There are %d words in: \"%s\"\n", numwords, ary);
- return 0;
- }
-
- Output:
-
- There are 10 words in: " test string with odd spacing "
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-